package com.fish.spider.core;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.fish.spider.excel.BaiduExcel;
import com.fish.spider.pojo.*;
import com.fish.spider.req.BaiduFeedParamReq;
import com.fish.spider.req.BaiduParamReq;
import com.fish.spider.util.BaiduDecryptionUtil;
import com.fish.spider.util.DateUtils;
import com.fish.spider.util.HttpUtil;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.jetbrains.annotations.NotNull;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
public class BaiduIndexSpider implements IBaiduIndexSpider {

    static final String BAIDU_CHECK_URL = "https://index.baidu.com/api/AddWordApi/checkWordsExists?word=";
    static final String BAIDU_SEARCH_URL = "https://index.baidu.com/api/SearchApi/index";
    static final String BAIDU_SEARCH_FEED_URL = "https://index.baidu.com/api/FeedSearchApi/getFeedIndex";
    static final String BAIDU_SEARCH_GET_LIVE_URL = "https://index.baidu.com/api/LiveApi/getLive";
    static final String PTBK_URL = "https://index.baidu.com/Interface/ptbk?uniqid=";

    @Override
    public String getLive(String content) throws Exception {
        String checkUrl = URLDecoder.decode(BAIDU_CHECK_URL + content, "UTF-8");
        //{"status":0,"data":{"result":[]},"logid":3574562701,"message":""}
        if (!checkWordsExists(checkUrl)) {
            log.info("数据不存在");
            return null;
        }
        Map<String, String> paramMap = Maps.newHashMap();
        paramMap.put("region", "0");
        paramMap.put("word", "[[%7B%22name%22:%22" + URLEncoder.encode(content, "UTF-8") + "%22,%22wordType%22:1%7D]]");
        StringBuilder tailParam = getTailParam(paramMap);
        String searchUrl = BAIDU_SEARCH_GET_LIVE_URL + tailParam.toString();
        String s = HttpUtil.httpGet(searchUrl, getHttpEntity());
        return null;
    }

    @Override
    public String getDefault(BaiduParamReq paramReq, BaiduFeedParamReq feedParamReq) throws Exception {
        BaiduParamReq baiduParamReq = new BaiduParamReq();
        String checkUrl = URLDecoder.decode(BAIDU_CHECK_URL + paramReq.getContent(), "UTF-8");
        if (!checkWordsExists(checkUrl)) {
            log.info("要查询的关键词:{}不存在", baiduParamReq.getContent());
            return null;
        }
        String indexSourceResult = getIndex(paramReq);
        BaiduResult baiduResult = decryptionIndex(indexSourceResult);

        String feedSourceResult = getFeed(feedParamReq);
        BaiduFeedResult baiduFeedResult = decryptionFeed(feedSourceResult);
        Map<String,List<BaiduExcel>> allSheetData = Maps.newHashMap();
        List<String> headerList = Lists.newArrayList();
        processExcelData(baiduResult,baiduFeedResult,paramReq.getContent(),allSheetData,headerList);
        writeMultipleSheetExcel(paramReq.getContent(),headerList,allSheetData);
        return null;
    }

    /**
     * 拆解明文数据成excel数据
     *
     * @param baiduResult
     * @param baiduFeedResult
     */
    private void processExcelData(BaiduResult baiduResult, BaiduFeedResult baiduFeedResult,String content,Map<String,List<BaiduExcel>> allSheetData,List<String> headerList) throws FileNotFoundException {

        List<String> contentList = Lists.newArrayList();
        Splitter.on(",").trimResults().split(content).forEach(contentList::add);
        Map<String, Map<String, List<BaiduExcel>>> indexMap = processExcelIndex(baiduResult, content);
        Map<String, Map<String, List<BaiduExcel>>> feedMap = processExcelFeed(baiduFeedResult, content);
        Map<String, List<BaiduExcel>> indexHeadMap = indexMap.get("head");
        Map<String, List<BaiduExcel>> feedHeadMap = feedMap.get("head");
        Map<String, List<BaiduExcel>> indexBodyMap = indexMap.get("body");
        Map<String, List<BaiduExcel>> feedBodyMap = feedMap.get("body");
        String fileName = content + System.currentTimeMillis() + ".xlsx";
        int cellCount = 0 ;
        for(String contentKey :contentList){
            List<BaiduExcel> sheetDataList = Lists.newArrayList();
            List<String> excelHeadList = Lists.newArrayList();
            excelHeadList.add("日期");
            excelHeadList.add("搜索整体日均值");
            excelHeadList.add("搜索移动日均值");
            excelHeadList.add("搜索整体同比");
            excelHeadList.add("搜索整体环比");
            excelHeadList.add("搜索移动同比");
            excelHeadList.add("搜索移动环比");
            excelHeadList.add("资讯日均值");
            excelHeadList.add("资讯同比");
            excelHeadList.add("资讯环比");
            if(headerList.size()==0){
                headerList.addAll(excelHeadList);
            }
            List<BaiduExcel> indexHeadExcelList = indexHeadMap.get(contentKey);
            List<BaiduExcel> feedHeadExcelList = feedHeadMap.get(contentKey);
            BaiduExcel baiduIndexHeadExcel = indexHeadExcelList.get(0);
            BaiduExcel baiduFeedHeadExcel = feedHeadExcelList.get(0);
            baiduIndexHeadExcel.setFeedAvg(baiduFeedHeadExcel.getFeedAvg());
            baiduIndexHeadExcel.setFeedQoq(baiduFeedHeadExcel.getFeedQoq());
            baiduIndexHeadExcel.setFeedYoy(baiduFeedHeadExcel.getFeedYoy());

            List<BaiduExcel> indexBodyList = indexBodyMap.get(contentKey);

            BaiduExcel beginDateExcel = indexBodyList.get(0);
            BaiduExcel endDateExcel = indexBodyList.get(indexBodyList.size()-1);
            String beginDateDesc = beginDateExcel.getDate();
            String endDateDesc = endDateExcel.getDate();
            StringBuilder sb = new StringBuilder();
            if(beginDateDesc.contains("~")){
                List<String> tempList1 = Lists.newArrayList();
                Splitter.on("~").trimResults().split(beginDateDesc).forEach(tempList1::add);
                sb.append(tempList1.get(0)).append("~");
                List<String> tempList2 = Lists.newArrayList();
                Splitter.on("~").trimResults().split(endDateDesc).forEach(tempList2::add);
                sb.append(tempList2.get(tempList2.size()-1));
            }else{
                sb.append(beginDateExcel.getDate()).append("~").append(endDateExcel.getDate());
            }
            baiduIndexHeadExcel.setDate(sb.toString());
            //第一行数据
            sheetDataList.add(baiduIndexHeadExcel);
            //第二行header
            sheetDataList.add(getSecondHeader());
            //依次补全数据
            List<BaiduExcel> feeBodyList = feedBodyMap.get(contentKey);
            int min = indexBodyList.size();
            if(indexBodyList.size() > feeBodyList.size()){
                min = feeBodyList.size();
            }
            for(int i = 0 ;i < min;i++){
                BaiduExcel baiduExcel = indexBodyList.get(i);
                baiduExcel.setAllYoy(feeBodyList.get(i).getAllYoy());
                baiduExcel.setAllQoq(feeBodyList.get(i).getAllQoq());
                sheetDataList.add(baiduExcel);
            }
            //差异数据
            if(min < indexBodyList.size()){
                for(int i = min ;i < indexBodyList.size();i++){
                    BaiduExcel baiduExcel = indexBodyList.get(i);
                    sheetDataList.add(baiduExcel);
                }
            }else if(min > indexBodyList.size()){
                for(int i = min ;i <= feeBodyList.size();i++){
                    BaiduExcel baiduExcel = feeBodyList.get(i);
                    sheetDataList.add(baiduExcel);
                }
            }
            allSheetData.put(contentKey,sheetDataList);
        }
    }

    public BaiduExcel getSecondHeader(){
        BaiduExcel baiduExcel = new BaiduExcel();
        baiduExcel.setDate("搜索指数");
        baiduExcel.setAllYoy("资讯指数");
        return baiduExcel;
    }

    public void writeExcel(String fileName, String content, List<String> header, List<BaiduExcel> dataList) throws FileNotFoundException {
        Path path = Paths.get("src/main/resources", fileName);
        FileOutputStream fileOutputStream = null;
        ExcelWriter excelWriter = null;
        try {
            fileOutputStream = new FileOutputStream(path.toFile().getAbsoluteFile());
            excelWriter = EasyExcel.write(fileOutputStream).build();
            List<List<String>> headList = Lists.newArrayList();
            header.forEach(v -> {
                ArrayList<@Nullable String> innerHeader = Lists.newArrayList();
                innerHeader.add(v);
                headList.add(innerHeader);
            });
//            headList.add(header);
            //创建sheet对象
            WriteSheet writeSheet = EasyExcel.writerSheet(content).head(headList).build();
            //写入数据
            excelWriter.write(dataList, writeSheet);
        } catch (Exception e) {
            log.info(e.getMessage());
        } finally {
            // 关闭 ExcelWriter
            if (excelWriter != null) {
                excelWriter.finish();
            }
            // 关闭 FileOutputStream
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void writeMultipleSheetExcel(String content, List<String> header, Map<String,List<BaiduExcel>> dataMap) throws FileNotFoundException {
        String fileName = content + System.currentTimeMillis() + ".xlsx";
        Path path = Paths.get("src/main/resources", fileName);
        FileOutputStream fileOutputStream = null;
        ExcelWriter excelWriter = null;
        try {
            fileOutputStream = new FileOutputStream(path.toFile().getAbsoluteFile());
            excelWriter = EasyExcel.write(fileOutputStream).build();
            List<List<String>> headList = Lists.newArrayList();
            header.forEach(v -> {
                ArrayList<@Nullable String> innerHeader = Lists.newArrayList();
                innerHeader.add(v);
                headList.add(innerHeader);
            });
//            headList.add(header);
            //创建sheet对象
            List<String> sheetNameList = Lists.newArrayList();
            Splitter.on(",").trimResults().split(content).forEach(sheetNameList::add);
            for(int i = 0;i<sheetNameList.size();i++){
                String sheetName = sheetNameList.get(i);
                WriteSheet writeSheet = EasyExcel.writerSheet(i,sheetNameList.get(i)).head(headList).build();
                //写入数据
                List<BaiduExcel> baiduExcels = dataMap.get(sheetName);
                excelWriter.write(baiduExcels, writeSheet);
            }
        } catch (Exception e) {
            log.info(e.getMessage());
        } finally {
            // 关闭 ExcelWriter
            if (excelWriter != null) {
                excelWriter.finish();
            }
            // 关闭 FileOutputStream
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public Map<String, Map<String, List<BaiduExcel>>> processExcelIndex(BaiduResult baiduResult, String content) {
        //获取表头数据
        BaiduBody baiduBody = baiduResult.getData();
        List<BaiduRatio> generalRatios = baiduBody.getGeneralRatio();
        List<Indexes> userIndexes = baiduBody.getUserIndexes();

        //从这里拆解出来，有多个sheet,用关键字作为区分他们的标记;
        Iterable<String> split = Splitter.on(",").trimResults().split(content);
        Map<String, Map<String, List<BaiduExcel>>> outMaps = Maps.newHashMap();
        //头部指标
        Map<String, List<BaiduExcel>> headerIndexMaps = processIndexHeader(generalRatios, split);

        //具体数据
        Map<String, List<BaiduExcel>> detailIndexMaps = processIndexDetailData(userIndexes, split);
        outMaps.put("head", headerIndexMaps);
        outMaps.put("body", detailIndexMaps);
        return outMaps;
    }

    public Map<String, Map<String, List<BaiduExcel>>> processExcelFeed(BaiduFeedResult baiduFeedResult, String content) {
        Map<String, Map<String, List<BaiduExcel>>> outMaps = Maps.newHashMap();
        //头部指标
        Map<String, List<BaiduExcel>> headerFeedMap = processFeedHeader(baiduFeedResult, content);
        Map<String, List<BaiduExcel>> detailFeedMap = processFeedDetail(baiduFeedResult, content);
        outMaps.put("head", headerFeedMap);
        outMaps.put("body", detailFeedMap);
        return outMaps;
    }



    private Map<String, List<BaiduExcel>> processIndexHeader(List<BaiduRatio> generalRatios, Iterable<String> split) {
        Map<String, List<BaiduExcel>> indexMaps = Maps.newHashMap();
        split.forEach(v -> {
            List<BaiduExcel> baiduExcelList = Lists.newArrayList();
            BaiduExcel baiduExcel = new BaiduExcel();
            //指标数据
            for (BaiduRatio baiduRatio : generalRatios) {
                List<BaiduWord> word = baiduRatio.getWord();
                for (BaiduWord baiduWord : word) {
                    if (v.equals(baiduWord.getName())) {

                        BaiduRatioDetail baiduRatioDetailAll = baiduRatio.getAll();
                        baiduExcel.setAllAvg(baiduRatioDetailAll.getAvg());
                        baiduExcel.setAllYoy(baiduRatioDetailAll.getYoy());
                        baiduExcel.setAllQoq(baiduRatioDetailAll.getQoq());

                        BaiduRatioDetail baiduRatioDetailWise = baiduRatio.getWise();
                        baiduExcel.setWiseAvg(baiduRatioDetailWise.getAvg());
                        baiduExcel.setWiseYoy(baiduRatioDetailWise.getYoy());
                        baiduExcel.setWiseQoq(baiduRatioDetailWise.getQoq());
                        baiduExcelList.add(baiduExcel);
                        indexMaps.put(v, baiduExcelList);
                    }
                }
            }
        });
        return indexMaps;
    }

    private Map<String, List<BaiduExcel>> processIndexDetailData(List<Indexes> userIndexes, Iterable<String> split) {
        Map<String, List<BaiduExcel>> indexDetailMaps = Maps.newHashMap();
        split.forEach(v -> {
            for (Indexes index : userIndexes) {
                String type = index.getType();
                int typeFlag = getTypeFlag(type);
                List<BaiduWord> word = index.getWord();
                for (BaiduWord baiduWord : word) {
                    if (v.equals(baiduWord.getName())) {
                        List<BaiduExcel> indexDetailList = Lists.newArrayList();
                        BaiduData allData = index.getAll();

                        String startDate = allData.getStartDate();
                        String endDate = allData.getEndDate();
                        //日为单位
                        List<String> dateList = Lists.newArrayList();
                        if (typeFlag == 0) {
                            dateList = DateUtils.generaDay(startDate, endDate);
                        } else if (typeFlag == 1) {//周为单位
                            dateList = DateUtils.generaWeek(startDate, endDate, true);
                        }
                        //all
                        String allDetailData = allData.getData();
                        List<String> allDetailDataList = Lists.newArrayList();
                        Splitter.on(",").trimResults().split(allDetailData).forEach(allDetailDataList::add);

                        //pc
                        String pcDetailData = index.getPc().getData();
                        List<String> pcDetailDataList = Lists.newArrayList();
                        Splitter.on(",").trimResults().split(pcDetailData).forEach(pcDetailDataList::add);

                        //wise
                        String wiseDetailData = index.getWise().getData();
                        List<String> wiseDetailDataList = Lists.newArrayList();
                        Splitter.on(",").trimResults().split(wiseDetailData).forEach(wiseDetailDataList::add);

                        for (int i = 0; i < dateList.size(); i++) {
                            BaiduExcel baiduExcel = new BaiduExcel();
                            baiduExcel.setDate(dateList.get(i));
                            baiduExcel.setAllAvg(allDetailDataList.get(i));
                            baiduExcel.setWiseAvg(wiseDetailDataList.get(i));
//                            baiduExcel.setPcAvg(wiseDetailDataList.get(i));
                            if(indexDetailMaps.get(v)==null){
                                indexDetailMaps.put(v,indexDetailList);
                            }
                            indexDetailMaps.get(v).add(baiduExcel);
                        }
                    }
                }
            }
        });
        return indexDetailMaps;
    }

    private static int getTypeFlag(String type) {
        int typeFlag = 0;
        if ("day".equals(type)) {
//                    excelHeadList.add("搜索日期(日)");
        } else if ("week".equals(type)) {
//                    excelHeadList.add("搜索日期(周)");
            typeFlag = 1;
        }
        return typeFlag;
    }

    public Map<String, List<BaiduExcel>> processFeedHeader(BaiduFeedResult baiduFeedResult, String content) {
        //获取表头数据
        BaiduFeedIndex baiduFeedIndex = baiduFeedResult.getData();
        List<BaiduFeedBody> bodyList = baiduFeedIndex.getIndex();
        //从这里拆解出来，有多个sheet,用关键字作为区分他们的标记;
        Iterable<String> split = Splitter.on(",").trimResults().split(content);
        Map<String, List<BaiduExcel>> outMaps = Maps.newHashMap();
        split.forEach(v -> {
            List<BaiduExcel> baiduExcelList = Lists.newArrayList();
            BaiduExcel baiduExcel = new BaiduExcel();
            //指标数据
            for (BaiduFeedBody baiduFeedBody : bodyList) {
                List<BaiduFeedKey> key = baiduFeedBody.getKey();
                for (BaiduFeedKey baiduFeedKey : key) {
                    if (v.equals(baiduFeedKey.getName())) {
                        BaiduFeedRatio generalRatio = baiduFeedBody.getGeneralRatio();
                        baiduExcel.setFeedAvg(generalRatio.getAvg());
                        baiduExcel.setFeedYoy(generalRatio.getYoy());
                        baiduExcel.setFeedQoq(generalRatio.getQoq());
                        baiduExcelList.add(baiduExcel);
                        outMaps.put(v, baiduExcelList);
                    }
                }
            }
        });
        return outMaps;
    }

    public Map<String, List<BaiduExcel>> processFeedDetail(BaiduFeedResult baiduFeedResult, String content) {
        Iterable<String> keys = Splitter.on(",").trimResults().split(content);
        Map<String, List<BaiduExcel>> outMaps = Maps.newHashMap();
        List<BaiduFeedBody> feedBodies = baiduFeedResult.getData().getIndex();
        keys.forEach(v -> {
            for (BaiduFeedBody baiduFeedBody : feedBodies) {
                String startDate = baiduFeedBody.getStartDate();
                String endDate = baiduFeedBody.getEndDate();
                List<BaiduFeedKey> baiduFeedKeys = baiduFeedBody.getKey();
                for (BaiduFeedKey baiduFeedKey : baiduFeedKeys) {
                    if (v.equals(baiduFeedKey.getName())) {
                        List<BaiduExcel> excelList = Lists.newArrayList();
                        String sourceFeedBody = baiduFeedBody.getData();
                        List<String> feedBodyDataList = Lists.newArrayList();
                        Splitter.on(",").trimResults().split(sourceFeedBody).forEach(feedBodyDataList::add);
                        String type = baiduFeedBody.getType();
                        int typeFlag = getTypeFlag(type);
                        List<String> dateList = Lists.newArrayList();
                        if (typeFlag == 0) {
                            dateList = DateUtils.generaDay(startDate, endDate);
                        } else if (typeFlag == 1) {//周为单位
                            dateList = DateUtils.generaWeek(startDate, endDate, true);
                        }
                        for (int i = 0; i < dateList.size(); i++) {
                            BaiduExcel baiduExcel = new BaiduExcel();
                            baiduExcel.setAllYoy(dateList.get(i));
                            baiduExcel.setAllQoq(feedBodyDataList.get(i));
                            if (outMaps.get(v) == null) {
                                outMaps.put(v, excelList);
                            }
                            excelList.add(baiduExcel);
                        }
                    }
                }
            }
        });
        return outMaps;
    }

    public Map<String, String> dealParam(BaiduParamReq baiduParamReq) {
        Map<String, String> paramMap = Maps.newHashMap();
        String content = baiduParamReq.getContent();
        paramMap.put("content", content);
        String area = baiduParamReq.getArea();
        //area
        if (StringUtils.isEmpty(area)) {
            paramMap.put("area", "0");
        } else {
            paramMap.put("area", area);
        }
        String startDate = baiduParamReq.getStartDate();
        String endDate = baiduParamReq.getEndDate();
        String days = baiduParamReq.getDays();
        if (StringUtils.isEmpty(startDate) || StringUtils.isEmpty(endDate)) {
            if (StringUtils.isEmpty(days)) {
                days = "90";
            }
            paramMap.put("days", days);
        }
        if (!StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)) {
            //check
            paramMap.put("startDate", startDate);
            paramMap.put("endDate", endDate);
        }
        return paramMap;
    }

    public Map<String, String> dealFeedParam(BaiduFeedParamReq req) {
        Map<String, String> paramMap = Maps.newHashMap();
        String content = req.getContent();
        paramMap.put("content", content);
        String area = req.getArea();
        //area
        if (StringUtils.isEmpty(area)) {
            paramMap.put("area", "0");
        } else {
            paramMap.put("area", area);
        }
        String startDate = req.getStartDate();
        String endDate = req.getEndDate();
        String days = req.getDays();
        if (StringUtils.isEmpty(startDate) || StringUtils.isEmpty(endDate)) {
            if (StringUtils.isEmpty(days)) {
                days = "90";
            }
            paramMap.put("days", days);
        }
        if (!StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)) {
            //check
            paramMap.put("startDate", startDate);
            paramMap.put("endDate", endDate);
        }
        return paramMap;
    }

    public String dealContent(String content) {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
//        paramMap.put("word", "[[%7B%22name%22:%22" + URLEncoder.encode(content, "UTF-8") + "%22,%22wordType%22:1%7D]]");
        Splitter.on(",").trimResults().split(content).forEach(v -> {
            try {
                sb.append("[%7B%22name%22:%22" + URLEncoder.encode(v, "UTF-8") + "%22,%22wordType%22:1%7D],");
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }
        });
        sb.setLength(sb.length() - 1);
        sb.append("]");
        return sb.toString();
    }

    public String getIndex(BaiduParamReq paramReq) throws Exception {
        Map<String, String> paramMap = Maps.newHashMap();
        paramMap.putAll(dealParam(paramReq));
        paramMap.put("word", dealContent(paramMap.get("content")));
        paramMap.remove("content");
        StringBuilder tailParam = getTailParam(paramMap);
        String searchUrl = BAIDU_SEARCH_URL + tailParam.toString();
        return HttpUtil.httpUriGet(searchUrl, getHttpEntity());
    }

    public String getFeed(BaiduFeedParamReq feedParamReq) throws Exception {
        Map<String, String> paramMap = Maps.newHashMap();
        paramMap.putAll(dealFeedParam(feedParamReq));
        paramMap.put("word", dealContent(paramMap.get("content")));
        paramMap.remove("content");
        StringBuilder tailParam = getTailParam(paramMap);
        String searchUrl = BAIDU_SEARCH_FEED_URL + tailParam.toString();
        return HttpUtil.httpUriGet(searchUrl, getHttpEntity());
    }

    public BaiduResult decryptionIndex(String content) throws Exception {
        Gson gson = new Gson();
        BaiduResult baiduResult = gson.fromJson(content, BaiduResult.class);
        String uniqid = baiduResult.getData().getUniqid();
        String ptbk = getPtbk(uniqid);
        BaiduBody data = baiduResult.getData();
        List<Indexes> userIndexes = data.getUserIndexes();
        for (Indexes indexes : userIndexes) {

            BaiduData allData = indexes.getAll();
            String all = BaiduDecryptionUtil.decryption(ptbk, allData.getData());
            allData.setData(all);

            BaiduData pcData = indexes.getPc();
            String pc = BaiduDecryptionUtil.decryption(ptbk, pcData.getData());
            pcData.setData(pc);

            BaiduData wiseData = indexes.getWise();
            String wise = BaiduDecryptionUtil.decryption(ptbk, wiseData.getData());
            wiseData.setData(wise);
        }
        return baiduResult;
    }

    public BaiduFeedResult decryptionFeed(String content) throws Exception {
        Gson gson = new Gson();
        BaiduFeedResult baiduFeedResult = gson.fromJson(content, BaiduFeedResult.class);
        String uniqid = baiduFeedResult.getData().getUniqid();
        String ptbk = getPtbk(uniqid);
        BaiduFeedIndex data = baiduFeedResult.getData();
        List<BaiduFeedBody> feeBodys = data.getIndex();
        for (BaiduFeedBody baiduFeedBody : feeBodys) {
            String decryption = BaiduDecryptionUtil.decryption(ptbk, baiduFeedBody.getData());
            baiduFeedBody.setData(decryption);
        }
        return baiduFeedResult;
    }

    @NotNull
    private static StringBuilder getTailParam(Map<String, String> paramMap) {
        StringBuilder tailParam = new StringBuilder();
        paramMap.forEach((k, v) -> {
            if (tailParam.length() == 0) {
                tailParam.append("?").append(k).append("=").append(v);
            } else {
                tailParam.append("&").append(k).append("=").append(v);
            }
        });
        return tailParam;
    }

    public static void main(String[] args) throws Exception {
        BaiduIndexSpider spider = new BaiduIndexSpider();
        BaiduParamReq paramReq = new BaiduParamReq();
        paramReq.setDays("180");
        paramReq.setContent("通货膨胀,黄金价格");
        BaiduFeedParamReq feedParamReq = new BaiduFeedParamReq();
        feedParamReq.setContent("通货膨胀,黄金价格");
        feedParamReq.setDays("180");
        spider.getDefault(paramReq,feedParamReq);

    }

    @Override
    public String get7Day(String content) throws Exception {
        return null;
    }

    @Override
    public String get30Day(String content) throws Exception {
        return null;
    }

    @Override
    public String get90Day(String content) throws Exception {
        return null;
    }

    @Override
    public String getHalfYear(String content) throws Exception {
        return null;
    }

    @Override
    public String getByDate(String content, String startDate, String endDate) throws Exception {
        return null;
    }

    @Override
    public String getPtbk(String uuid) throws Exception {
        String result = HttpUtil.httpGet(PTBK_URL + uuid, getHttpEntity());
        Gson gson = new Gson();
        Map<String, String> map = gson.fromJson(result, Map.class);
        return map.get("data");
    }

    public boolean checkWordsExists(String url) {
        String result = HttpUtil.httpGet(url, getHttpEntity());
        Gson gson = new Gson();
        Map<String, Object> map = gson.fromJson(result, new TypeToken<Map<String, Object>>() {
        }.getType());
        if (null != map) {
            Object status = map.get("status");
            if (status != null) {
                if (0 == (double) status) {
                    Object data = map.get("data");
                    Map<String, Object> dataMap = (Map) data;
                    Object addWordsNum = dataMap.get("addWordsNum");
                    if (null == addWordsNum) {
                        log.info("匹配关键词成功");
                        return true;
                    }
                }
            }
        }
        log.info("匹配关键词失败");
        return false;
    }

    public static HttpEntity getHttpEntity() {
        HttpHeaders headers = new HttpHeaders();
        List<String> cookies = Lists.newArrayList();
        // 登录获取Cookie 这里是直接给Cookie，可使用下方的login方法拿到Cookie给入
        // 把上一步获取的cookie，在请求header里面传入
        cookies.add("newlogin=1; BIDUPSID=785DFFCCD1E3F4C69AA8A47E9CAA3AB7; PSTM=1717151865; Hm_lvt_d101ea4d2a5c67dab98251f0b5de24dc=1717151735,1717206356; BDUSS=1nZVY3Um53aG1IcGJDR0RVYlZ0YVZhR2pnR1pRSjVGVGMyeFZ-d3FFN0dDNEptRUFBQUFBJCQAAAAAAAAAAAEAAADtnYAObDkzOTgyNTkyNgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMZ-WmbGflpmNy; bdindexid=1d4qji720hrndnbqtt8vvc70k0; SIGNIN_UC=70a2711cf1d3d9b1a82d2f87d633bd8a04672191900SX7OWWnrskEOBfnRPZD7j9l%2FdKoYnXR0hrlrSTNqSmvKGFMX8wEabD1OeEmjHlML72mmWxUdSkRXKwfxvDtv9ue3RHBba20ke63Pyy5o2NMmx2eW9yUP5sstgKBmrq7WNl3s%2BTB%2BMwUDfLdU0DLil8JfhoMEFEXxFxaTY%2FWwbMGBWEXL6E7bk%2FjlSgtoFYFW5RlOn0eZetYSODdJxve6OfWRBHCM6pGs2lYOUU9dvuOD%2BWW10tt39yWQlshQ3Se%2Fr8bjG5N2lXT8zWvqeFhh1g%3D%3D99450801218730186301794146266602; __cas__rn__=467219190; __cas__st__212=90ad796d6651320a0a9868bca6ee0d8c9c3a38744becf48c4847dd10b1d576f34ea888d5a414d821aee35a7a; __cas__id__212=56050408; CPTK_212=686272172; CPID_212=56050408; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; delPer=1; PSINO=7; BAIDUID=717A60D6271FA8725635524A5905A0F6:FG=1; H_PS_PSSID=60269_60273_60299; BAIDUID_BFESS=717A60D6271FA8725635524A5905A0F6:FG=1; BA_HECTOR=2ga48h008180850h0001200le6lqfs1j5l2k01u; ZFY=3tgwfw:BwPw1UJmfbUzSAu5ebWSExaIiit9se7nI8emM:C; Hm_lpvt_d101ea4d2a5c67dab98251f0b5de24dc=1717248746; RT=\"z=1&dm=baidu.com&si=b996890f-5edc-47a2-aec7-47c91d334d3c&ss=lww09o24&sl=0&tt=0&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf\"; BDUSS_BFESS=1nZVY3Um53aG1IcGJDR0RVYlZ0YVZhR2pnR1pRSjVGVGMyeFZ-d3FFN0dDNEptRUFBQUFBJCQAAAAAAAAAAAEAAADtnYAObDkzOTgyNTkyNgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMZ-WmbGflpmNy; ab_sr=1.0.1_MDcyNThiZTg1OTQ0ZDU2YTEyYWI2MDMxYTVjMjkyNzlhM2UzZjczMTJmYzQxYjA0ODdhMzg5N2FlY2MwN2MwMTFmYzBiNGE0MzZiMDMyNWNlYzZmMDdkNzMyMmRjYTBiODExZTU0NzU5MjhlN2FhN2M0YTk4NTAyODNjMWUxNDg2YjIyNGUyY2I1ZDA2Y2Y2MmJjY2QyMjdkYjhjYzkwNw==");
        List<String> userAgent = Lists.newArrayList();
        userAgent.add("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0");
        List<String> host = Lists.newArrayList();
        host.add("index.baidu.com");
        List<String> referer = Lists.newArrayList();
        referer.add("https://index.baidu.com/v2/main/index.html");
        List<String> accept = Lists.newArrayList();
        accept.add("application/json, text/plain, */*");
        List<String> acceptEncoding = Lists.newArrayList();
        acceptEncoding.add("gzip, deflate, br, zstd");
        List<String> connection = Lists.newArrayList();
        connection.add("keep-alive");
        List<String> cipherText = Lists.newArrayList();
        cipherText.add("1717239914174_1717248747487_1ZziDXARA31F2GhLKYXYWWwcfMxEN+P+CRXHMc5Z40FdyoEDw1uYY4mIMd0i/3dKboOn4Ir5TtqAGSNEQTX0/RwcNSChkcDeXsVHewYBCjyCJMJqHSTFdXYrXaBsPIhOH1S4KaPha38in4eUsHxVtcVM8ddFVouJNpG2nHwV5m0ZGuy9ET6G/CN0c8mYv6x3IouXRY3g6euMCB9lJGcdeTy6OF80ONwiTTGTGcNnP5G12qJ2D+vQdVoo8IxT2Mdm+A9vumJwEFwtKUFX+Cswn1yvjh4GmT98o0s49ZL6b6yMxsiFH+iadRhZqp/f9fp2p8x+3ZQtw0Z32xOXlNkCz3AbKS8mrljro/HdzTuq5biEXQCW+JKKrCEzylUd55+Vu109ZhSK6u/FLLSdZXhq8g3MDQJuneBDI7a1sTnHQHU=");
        // 将cookie存入头部
        headers.put(HttpHeaders.COOKIE, cookies);
        headers.put(HttpHeaders.HOST, host);
        headers.put(HttpHeaders.USER_AGENT, userAgent);
        headers.put(HttpHeaders.REFERER, referer);
        headers.put(HttpHeaders.ACCEPT, accept);
        headers.put(HttpHeaders.ACCEPT_ENCODING, acceptEncoding);
        headers.put(HttpHeaders.CONNECTION, connection);
        headers.put("Cipher-Text", cipherText);
        headers.setContentType(MediaType.APPLICATION_JSON);
        Map<String, Object> paramMap = Maps.newHashMap();
        return new HttpEntity<>(paramMap, headers);
    }
}
